﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIPanel_Setting : UIPanelBase {
    Transform panelRoot;
    Transform btnSound;
    Transform btnMusic;
    Transform btnHelp;
    Transform btnMe;
    Transform btnBack;

    public override void OnInit(Transform root)
    {
        base.OnInit(root);
        panelRoot = root;
        InitGameObj();
        InitEvent();
    }

    void InitGameObj()
    {
        btnSound = panelRoot.Find("btn_sound");
        btnMusic = panelRoot.Find("btn_music");
        btnHelp = panelRoot.Find("btn_help");
        btnMe = panelRoot.Find("btn_me");
        btnBack = panelRoot.Find("btn_back");
        SetSwitchUIState(btnSound, PlayerPrefs.GetInt("sound")==0);
        SetSwitchUIState(btnMusic, PlayerPrefs.GetInt("music") ==0);
    }
    void InitEvent()
    {
        Button btn_sound = btnSound.GetComponent<Button>();
        btn_sound.onClick.RemoveAllListeners();
        btn_sound.onClick.AddListener(SoundSwitch);
        Button btn_music = btnMusic.GetComponent<Button>();
        btn_music.onClick.RemoveAllListeners();
        btn_music.onClick.AddListener(MusicSwitch);
        Button btn_help = btnHelp.GetComponent<Button>();
        btn_help.onClick.RemoveAllListeners();
        btn_help.onClick.AddListener(Help);
        Button btn_me = btnMe.GetComponent<Button>();
        btn_me.onClick.RemoveAllListeners();
        btn_me.onClick.AddListener(Me);
        Button btn_back = btnBack.GetComponent<Button>();
        btn_back.onClick.RemoveAllListeners();
        btn_back.onClick.AddListener(Back);
    }

    void SoundSwitch()
    {
        bool curState = PlayerPrefs.GetInt("sound")==0;
        curState = !curState;
        AudioController.SetMusic(SoundSettingType.sound, curState);
        SetSwitchUIState(btnSound, curState);
    }

    void MusicSwitch()
    {
        bool curState = PlayerPrefs.GetInt("music") == 0;
        curState = !curState;
        AudioController.SetMusic(SoundSettingType.music, curState);
        SetSwitchUIState(btnMusic, curState);
    }

    void SetSwitchUIState(Transform btnRoot,bool curState)
    {
        
        Image imgOn = btnRoot.Find("state_on").GetComponent<Image>();
        Image imgOff = btnRoot.Find("state_off").GetComponent<Image>();
        imgOn.enabled = false;
        imgOff.enabled = false;
        if (curState)//on
        {
            imgOn.enabled = true;
        }
        else
        {
            imgOff.enabled = true;
        }
    }

    void Help()
    {
        UIManager.ShowUI<UIPanel_Help>();
    }

    void Me()
    {
        Application.OpenURL("http://39.108.9.191");
    }

    void Back()
    {
        UIManager.CloseUI<UIPanel_Setting>();
    }


    public override void Update()
    {
        base.Update();
    }
    public override void OnClose()
    {
        base.OnClose();
    }
}
